package com.sansui.blog.controller.admin.page;

import com.sansui.blog.pojo.ResponseResult;
import com.sansui.blog.pojo.User;
import com.sansui.blog.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @name: TypeController
 * @author: Sansui
 * @Software: IDEA
 * @Blog: https://www.cnblogs.com/sansui6/
 * @date: 2022-03-20 22:47
 **/
//后台分类
@Controller
@RequestMapping("/admin")
public class TypeController {

    @Autowired
    private TypeService typeService;

    //  后台分类管理页面
    @GetMapping("page/types")
    public String adminTypes(){
        return "admin/page/types";
    }

    //  获取分类接口数据
    @PostMapping("page/getTypeList")
    @ResponseBody
    public Map<String, Object> getTypeList(Integer page,Integer limit){
        Map<String, Object> map = typeService.getAllTypeByPage(page,limit);
        return map;
    }

    //  获取分类接口数据
    @PostMapping("page/getTypeList/blogs")
    @ResponseBody
    public Map<String, Object> getTypeListByblogs(@RequestParam Map<String, String> paraMap){
        Map<String, Object> map = new HashMap<>(16);
        if(paraMap.get("keyword")==null){
            Integer page = Integer.valueOf(paraMap.get("page"));
            Integer limit = Integer.valueOf(paraMap.get("limit"));
            map = typeService.getAllTypeByPage(page,limit);
        }else {
            map = typeService.TypeSearchByName(paraMap.get("keyword"));
        }
        return map;
    }


    //  打开添加分类页面
    @GetMapping("page/type/add")
    public String addTypeHtml(){
        return "admin/page/type/add";
    }

    //  打开编辑分类页面
    @GetMapping("page/type/edit")
    public String editTypeHtml(){
        return "admin/page/type/edit";
    }


    //  根据分类id删除分类
    @PostMapping("page/deleteType")
    @ResponseBody
    public Map<String, Object> deleteType(Long typeId){
        Map<String, Object> map = typeService.deleteType(typeId);
        return map;
    }

    //  根据分类id批量删除分类
    @PostMapping("page/deleteTypeByIds")
    @ResponseBody
    public Map<String, Object> deleteTypeByIds(String typeIds){
        List<String> delList = new ArrayList<>();
        String[] tIds = typeIds.split(",");
        for (String str : tIds) {
            delList.add(str);
        };
        Map<String, Object> map = typeService.deleteTypeByIds(delList);
        return map;
    }


    //  根据name获取分类信息
    @PostMapping("page/TypeSearchByName")
    @ResponseBody
    public Map<String, Object> TypeSearchByName(String typeName){
        Map<String, Object> map = typeService.TypeSearchByName(typeName);
//        System.out.println(map);
        return map;
    }

    //  添加分类
    @PostMapping("page/type/add")
    @ResponseBody
    public Map<String, Object> addType(String typeName, HttpSession session){
//        User user = (User) session.getAttribute("user");
        Map<String,Object> data = (Map<String, Object>) session.getAttribute("user");
        Map<String, Object> map = typeService.addType(typeName,String.valueOf(data.get("username")));
        return map;
    }

    //  修改分类
    @PostMapping("page/type/update")
    @ResponseBody
    public Map<String, Object> updateType(String typeName, Long typeId,HttpSession session){
//        User user = (User) session.getAttribute("user");
        Map<String,Object> data = (Map<String, Object>) session.getAttribute("user");
        Map<String, Object> map = typeService.updateType(typeName,typeId,String.valueOf(data.get("username")));
        return map;
    }




}
